﻿#include "sickui.h"
#include "ui_sickui.h"

sickUI::sickUI(QString account,QSqlDatabase* db,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::sickUI)
{
    ui->setupUi(this);

    m_db = *db;//初始化数据库连接
    m_account = account;//初始化登陆id

    this->setCentralWidget(ui->stackedWidget);

    iniUI();//初始化

    ui->stackedWidget->setCurrentWidget(ui->pagMain);//设置主界面为初始界面
}

sickUI::~sickUI()
{
    delete ui;
}

void sickUI::on_actReserve_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagReserve);
}


void sickUI::on_actChange_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagChange);
}


void sickUI::on_actLookDoctor_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagLookDoctor);
}


void sickUI::on_actMyCase_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMyCase);
}


void sickUI::on_actQuit_triggered()
{
    this->close();
}


void sickUI::on_actMain_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void sickUI::on_pushButton_clicked()
{
    ui->actReserve->trigger();
}


void sickUI::on_pushButton_2_clicked()
{
    ui->actChange->trigger();
}


void sickUI::on_pushButton_3_clicked()
{
    ui->actLookDoctor->trigger();
}


void sickUI::on_pushButton_4_clicked()
{
    ui->actMyCase->trigger();
}


void sickUI::on_pushButton_5_clicked()
{
    this->close();
}

void sickUI::iniUI()
{
    //初始化界面文本显示
    QSqlQuery query;
    query.exec("select * from tb_sick where account='"+m_account+"';");
    if(query.size() == 0){//如果没找到该账号
        QMessageBox::warning(0,"Warning","Initialization failure.");
        return;
    }

    query.first();
    if(query.isValid()){

        //main界面
        ui->labId->setText(query.value("id").toString());
        ui->labAccount->setText(query.value("account").toString());
        ui->labAge->setText(query.value("age").toString());
        ui->labSex->setText(query.value("sex").toString());
        ui->labPhone->setText(query.value("phone").toString());
        ui->labAddress->setText(query.value("address").toString());

        //预约界面
        ui->labIdReserve->setText(query.value("id").toString());
        ui->labAccountReserve->setText(query.value("account").toString());

        //反馈界面
        ui->labIdFeedback->setText(query.value("id").toString());
        ui->labAccountFeedback->setText(query.value("account").toString());

        //修改界面
        ui->labIdChange->setText(query.value("id").toString());
        ui->editPswd->setText(query.value("pswd").toString());
        ui->labAccountChange->setText(query.value("account").toString());
        ui->spinAgeChange->setValue(query.value("age").toString().toInt());
        if(query.value("sex").toString() == "Boy"){
            ui->combSexChange->setCurrentIndex(0);
        }else{
            ui->combSexChange->setCurrentIndex(1);
        }
        ui->editPhoneChange->setText(query.value("phone").toString());
        ui->editAddressChange->setText(query.value("address").toString());

    }

    m_doctorModel = new QSqlTableModel(this);//医生信息数据模型
    m_caseModel = new QSqlTableModel(this);//客户病例信息数据模型

    //设置表名
    m_doctorModel->setTable("tb_doctor");
    m_caseModel->setTable("tb_case");

    //设置视图自动拉伸和不可编辑
    ui->tvLookDoctor->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvMyCse->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvLookDoctor->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tvMyCse->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //同步数据到模型
    m_doctorModel->select();
    m_caseModel->select();

    //绑定模型和视图
    ui->tvLookDoctor->setModel(m_doctorModel);
    ui->tvMyCse->setModel(m_caseModel);

    //隐藏医生密码列
    ui->tvLookDoctor->hideColumn(2);

    //修改表头
//    m_doctorModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_doctorModel->setHeaderData(1,Qt::Horizontal,"账号");
//    m_doctorModel->setHeaderData(3,Qt::Horizontal,"年龄");
//    m_doctorModel->setHeaderData(4,Qt::Horizontal,"性别");
//    m_doctorModel->setHeaderData(5,Qt::Horizontal,"电话");
//    m_doctorModel->setHeaderData(6,Qt::Horizontal,"住址");
//    m_doctorModel->setHeaderData(7,Qt::Horizontal,"类型");

//    m_caseModel->setHeaderData(0,Qt::Horizontal,"编号");
//    m_caseModel->setHeaderData(1,Qt::Horizontal,"病人编号");
//    m_caseModel->setHeaderData(2,Qt::Horizontal,"病人账号");
//    m_caseModel->setHeaderData(3,Qt::Horizontal,"医生编号");
//    m_caseModel->setHeaderData(4,Qt::Horizontal,"医生账号");
//    m_caseModel->setHeaderData(5,Qt::Horizontal,"描述");
//    m_caseModel->setHeaderData(6,Qt::Horizontal,"处方");

    //处理病例视图,只显示该用户的病例
    for(int i = 0;i < m_caseModel->rowCount(); ++i){
        ui->tvMyCse->hideRow(i);
        if(m_caseModel->data(m_caseModel->index(i,2)).toString() == m_account){
            ui->tvMyCse->showRow(i);
        }
    }

}




void sickUI::on_btnOkReserve_clicked()
{
    QSqlQuery query;
    if(query.exec("insert into tb_reserve(sickid,sickaccount,time,type) values('"+ui->labId->text()+"','"+ui->labAccount->text()+"','"+ui->timeEditReserve->text()+"','"+ui->combTypeRese->currentText()+"');")){
        QMessageBox::information(0,"Information","Successful appointment!");
    }else{
        QMessageBox::warning(0,"Warning","Reservatin failure!");
    }
}


void sickUI::on_actFeedback_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagFeedba);
}


void sickUI::on_btnSubmit_clicked()
{
    QSqlQuery query;
    if(query.exec("insert into tb_feedback(sickid,sickaccount,content) values('"+ui->labIdFeedback->text()+"','"+ui->labAccountFeedback->text()+"','"+ui->editContentFeedback->toPlainText()+"');")){
        QMessageBox::information(0,"Information","Submit Successfully!");
    }else{
        QMessageBox::warning(0,"Warning","Submit failure.");
    }
}


void sickUI::on_btnFeedBack_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagFeedba);
}


void sickUI::on_btnReset_clicked()
{
    QSqlQuery query;
    query.exec("select * from tb_sick where account='"+m_account+"';");
    if(query.size() == 0){//如果没找到该账号
        QMessageBox::warning(0,"Warning","Initialization failure..");
        return;
    }

    query.first();
    if(!query.isValid()){
        QMessageBox::warning(0,"Warning","Reset failure.");
        return;
    }
    ui->labIdChange->setText(query.value("id").toString());
    ui->labAccountChange->setText(query.value("account").toString());
    ui->editPswd->setText(query.value("pswd").toString());
    ui->spinAgeChange->setValue(query.value("age").toString().toInt());
    if(query.value("sex").toString() == "Boy"){
        ui->combSexChange->setCurrentIndex(0);
    }else{
        ui->combSexChange->setCurrentIndex(1);
    }
    ui->editPhoneChange->setText(query.value("phone").toString());
    ui->editAddressChange->setText(query.value("address").toString());

}


void sickUI::on_btnSubmitChange_clicked()
{
    if(ui->editPswd->text().isEmpty()){
        QMessageBox::warning(0,"Warning","The password cannot be empty.");
        return;
    }
    QSqlQuery query;
    if(query.exec("update tb_sick set pswd='"+ui->editPswd->text()+"',age='"+ui->spinAgeChange->text()+"',sex='"+ui->combSexChange->currentText()+"',phone='"+ui->editPhoneChange->text()+"',address='"+ui->editAddressChange->text()+"' where account='"+m_account+"';")){
        QMessageBox::information(0,"Information","Modify successfully!");

        //修改界面信息
        QSqlQuery query;
        query.exec("select * from tb_sick where account='"+m_account+"';");
        if(query.size() == 0){//如果没找到该账号
            QMessageBox::warning(0,"Warning","Initialization failure.");
            return;
        }

        query.first();
        if(query.isValid()){

            //main界面
            ui->labId->setText(query.value("id").toString());
            ui->labAccount->setText(query.value("account").toString());
            ui->labAge->setText(query.value("age").toString());
            ui->labSex->setText(query.value("sex").toString());
            ui->labPhone->setText(query.value("phone").toString());
            ui->labAddress->setText(query.value("address").toString());

            //预约界面
            ui->labIdReserve->setText(query.value("id").toString());
            ui->labAccountReserve->setText(query.value("account").toString());

            //反馈界面
            ui->labIdFeedback->setText(query.value("id").toString());
            ui->labAccountFeedback->setText(query.value("account").toString());
        }
        return;
    }else{
        QMessageBox::warning(0,"Warning","Modify failure.");
        return;
    }
}

